package sql;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlManager {
    
    public static final File INIT = new File("sql/init.sql");
    public static final String DATABASE = "jdbc:sqlite:sql/database.db";

    public static final Statement createStatement () throws SQLException {
        return DriverManager.getConnection(DATABASE).createStatement();
    }

    public static void init () throws SQLException, IOException {
        Statement statement = createStatement();
        InputStream in = new FileInputStream(INIT);
        byte[] b = new byte[in.available()];
        in.read(b);
        in.close();
        String[] sqls = new String(b, "UTF-8").split(";");
        for (String sql : sqls) {
            try {
                statement.execute(sql);
            } catch (Exception e) {
                System.out.println(sql);
                e.printStackTrace();
            }
        }
        statement.close();
    }

}
